package grape.workflow.rest.flowable.ui.admin;


import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.flowable.ui.admin.domain.EndpointType;
import org.flowable.ui.admin.domain.ServerConfig;
import org.flowable.ui.admin.rest.client.AbstractClientResource;
import org.flowable.ui.admin.service.engine.exception.FlowableServiceException;
import org.flowable.ui.common.service.exception.BadRequestException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 由于原始类{@link org.flowable.ui.admin.rest.client.CmmnTasksClientResource} 中的 CmmnTaskService与cmmn引擎命名冲突导致注入不上
 * Created by yangwei
 * Created at 2020/6/5 18:46
 */
@RestController
@RequestMapping("/app")  // flowable原生扩展，不加前缀
public class CmmnTasksClientResource extends AbstractClientResource {

    private static final Logger LOGGER = LoggerFactory.getLogger(org.flowable.ui.admin.rest.client.CmmnTasksClientResource.class);

    @Autowired
    protected CmmnTaskService clientService;

    @PostMapping(value = "/rest/admin/cmmn-tasks", produces = "application/json")
    public JsonNode listTasks(@RequestBody ObjectNode requestNode) {
        ServerConfig serverConfig = retrieveServerConfig(EndpointType.CMMN);
        JsonNode resultNode;
        try {
            resultNode = clientService.listTasks(serverConfig, requestNode);
        } catch (FlowableServiceException e) {
            LOGGER.error("Error getting tasks", e);
            throw new BadRequestException(e.getMessage());
        }

        return resultNode;
    }
}
